import subprocess
import time

if __name__ == '__main__':
    base_exe = "./gen_data"
    opti_exe = "./optimized"
    output_file_path = "./output_rest.txt"
    nlist = {2**12,2**13,2**14,2**15,2**16}
    tmaxlist = {2**15,2**16,2**17,2**18,2**19}

    for n in nlist:
        for tmax in tmaxlist:
            # 使用subprocess运行可执行文件，并将输出重定向到文件
            with open(output_file_path, "a") as output_file:
                try:
                    # 运行可执行文件，并将标准输出和标准错误都重定向到文件
                    result = subprocess.run([base_exe,str(n),str(tmax)], stdout=output_file, stderr=subprocess.STDOUT, text=True, check=True)
                    time.sleep(60)
                    result = subprocess.run([opti_exe,str(n),str(tmax)], stdout=output_file, stderr=subprocess.STDOUT, text=True, check=True)
                    time.sleep(60)
                    # 如果需要捕获返回码，可以使用result.returncode
                    print(f"Executable ran successfully with return code: {result.returncode}")
                except subprocess.CalledProcessError as e:
                    # 如果可执行文件返回非零退出码，会抛出CalledProcessError
                    print(f"Executable failed with return code: {e.returncode}")
                    print(f"Output: {e.output}")

